package com.game.logic.roles;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.constant.AIDCFG;
import com.coment.cfg.luban.roles.CharacterBaseAttributeConfig;
import com.coment.cfg.luban.roles.NumericalConfig;
import com.coment.cfg.luban.roles.TbCharacterBaseAttributeConfig;
import com.coment.game.fight.Attribute;
import com.coment.game.fight.AttributeManagerLogic;
import com.coment.game.fight.Attrs;
import com.coment.po.roles.RolePanel;
import com.coment.po.roles.Roles;
import com.xs.action.exection.ActionException;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/// 角色养成逻辑
@Slf4j
public class RolesCultivateLogic {
    /// 最大星级
    private static final double MAX_STAR_LEVEL = 200.0;


    // ----------------------------- 养成

    /// 突破升级
    public static Attrs tryBreakthrough(Roles roles) {
        Attrs attrsGlobal = new Attrs();
        Integer userInfoId = roles.getUserInfoId();
        Integer cardId = roles.getCardId();
        RolePanel rolePanel = roles.getRolePanel();
        double level = rolePanel.cultivatePanel.base.get(AIDCFG.level);
        double exp = rolePanel.cultivatePanel.base.get(AIDCFG.exp);

        TbCharacterBaseAttributeConfig config = Tables.cfg.get_tbcharacterbaseattributeconfig();

        CharacterBaseAttributeConfig t = config.get((int) (level + 0.0D));
        long currentLevelExp = t.totalExp;

        CharacterBaseAttributeConfig t1 = config.get((int) (level + 1.0D));
        long nextLevelExp = t1.totalExp;

        if (exp < nextLevelExp) {
            String info = "玩家:%s角色:%s差%s经验%s--%s".formatted(userInfoId, cardId, nextLevelExp - exp, exp, nextLevelExp);
            throw new ActionException(info);
        }

        if (level < 40) {
            log.info("等级不足40级,不需要渡劫");
            attrsGlobal = tryUpgrade(roles, 1);
        } else if ((level) % 10 != 1) {
            log.info("非整10级突破,直接升级");
            attrsGlobal = tryUpgrade(roles, 1);
        } else {
            double baseProb = rolePanel.cultivatePanel.getValue().get(AIDCFG.BreakthroughProbability);
            int failCount = rolePanel.cultivatePanel.getValue().getOrDefault(AIDCFG.BreakthroughFailCount, 0D).intValue();
            double finalProb = baseProb * (1.0D + failCount * 0.1D) / 100.0D;
            log.info("用户:{},角色:{},基础概率:{},失败次数:{},最终概率:{}", userInfoId, cardId, baseProb, failCount, finalProb);
            if (finalProb > Math.random()) {
                attrsGlobal = tryUpgrade(roles, 1);
            } else {
                double punishmentExperience = (double) (nextLevelExp - currentLevelExp);
                rolePanel.cultivatePanel.base.merge(AIDCFG.exp, punishmentExperience, Double::sum);
                rolePanel.cultivatePanel.base.merge(AIDCFG.BreakthroughFailCount, 1.0D, Double::sum);
                log.info("渡劫失败,角色:{},经验:{},失败次数:{}", cardId, punishmentExperience, failCount);
                roles.setRolePanel(rolePanel);
            }
        }

        return attrsGlobal;
    }

    /// 升级
    public static Attrs tryUpgrade(Roles roles, Integer lv) {
        Double level = roles.rolePanel.cultivatePanel.base.get(AIDCFG.level);
        level += lv;
        roles.rolePanel.cultivatePanel.base.put(AIDCFG.level, level);
        return Attribute.obtainXTQYValue(roles.cardId, level);
    }

    /// 升星
    public static Attrs tryRisingStar(Roles roles) {
        Double currentStar = roles.rolePanel.cultivatePanel.base.get(AIDCFG.star);
        roles.rolePanel.cultivatePanel.getValue();
        if ((currentStar + 1) > MAX_STAR_LEVEL) {
            log.error("角色已满星{}", Attribute.getLogInfo(roles.rolePanel.cultivatePanel.getValue()));
            return Attribute.obtainRolesStarValue(roles.cardId, currentStar);
        }
        currentStar++;
        // 升星处理
        roles.rolePanel.cultivatePanel.base.put(AIDCFG.star, currentStar);
        return Attribute.obtainRolesStarValue(roles.cardId, currentStar);
    }

    /// 重新计算战斗力
    public static Double tryCountFightingCapacity(Roles roles) {
        Attrs attrs = roles.rolePanel.cultivatePanel.getValue();
        Attrs cfgValue = AttributeManagerLogic.obtainCfgAttrs(Tables.cfg.get_tbstrengthaddbyattrscfg().get_data());

        ArrayList<Object> objects = new ArrayList<>();
        double fightingCapacity = 0.0D;
        for (Map.Entry<Integer, Double> entry : cfgValue.entrySet()) {
            Integer id = entry.getKey();

            NumericalConfig numericalConfig = Tables.cfg.get_tbnumericalconfig().get(id);
            double initialValue = numericalConfig.InitialValue / numericalConfig.NumType;

            Double addValue = entry.getValue();
            double addAttrs = attrs.get(id) - initialValue;
            double addFightingCapacity = addAttrs * addValue;
            fightingCapacity += addFightingCapacity;

            if (addFightingCapacity> 0) {
                HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
                objectObjectHashMap.put("AttributeName", numericalConfig.AttributeName);
                objectObjectHashMap.put("NumericalID", numericalConfig.NumericalID);
                objectObjectHashMap.put("initialValue", initialValue);
                objectObjectHashMap.put("addAttrs", addAttrs);
                objectObjectHashMap.put("增加战斗力", addFightingCapacity);
                objects.add(objectObjectHashMap);
            }
        }
//        log.info(JSONObject.toJSON(objects).toString());
        return fightingCapacity;
    }
}
